package com.gxa.jd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.jd.mapper.RoomMapper;
import com.gxa.jd.pojo.Room;
import com.gxa.jd.service.RoomService;
import com.gxa.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    /**
     * 返回全部记录
     *
     * @return
     */
    @Override
    public List<Room> findAll() {
        return roomMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    @Override
    public PageResult<Room> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Room> rooms = (Page<Room>) roomMapper.selectAll();
        return new PageResult<Room>(rooms.getTotal(), rooms.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    @Override
    public List<Room> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return roomMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageResult<Room> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Room> rooms = (Page<Room>) roomMapper.selectByExample(example);
        return new PageResult<Room>(rooms.getTotal(), rooms.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    @Override
    public Room findById(Long id) {
        return roomMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param room
     */
    @Override
    public void add(Room room) {
        roomMapper.insert(room);
    }

    /**
     * 修改
     *
     * @param room
     */
    @Override
    public void update(Room room) {
        roomMapper.updateByPrimaryKeySelective(room);
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        roomMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Room.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 房型名称
            if (searchMap.get("roomName") != null && !"".equals(searchMap.get("roomName"))) {
                criteria.andLike("roomName", "%" + searchMap.get("roomName") + "%");
            }
            // 房间图片
            if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
                criteria.andLike("image", "%" + searchMap.get("image") + "%");
            }
            // 配套服务
            if (searchMap.get("images") != null && !"".equals(searchMap.get("images"))) {
                criteria.andLike("images", "%" + searchMap.get("images") + "%");
            }
            // 房间介绍
            if (searchMap.get("introduction") != null && !"".equals(searchMap.get("introduction"))) {
                criteria.andLike("introduction", "%" + searchMap.get("introduction") + "%");
            }
            // 标签
            if (searchMap.get("label") != null && !"".equals(searchMap.get("label"))) {
                criteria.andLike("label", "%" + searchMap.get("label") + "%");
            }

            // 可住人数
            if (searchMap.get("people") != null) {
                criteria.andEqualTo("people", searchMap.get("people"));
            }
            // 可否加床
            if (searchMap.get("bed") != null) {
                criteria.andEqualTo("bed", searchMap.get("bed"));
            }
            // 是否含早
            if (searchMap.get("breakfast") != null) {
                criteria.andEqualTo("breakfast", searchMap.get("breakfast"));
            }
            // 审核状态
            if (searchMap.get("status") != null) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            // 库存
            if (searchMap.get("num") != null) {
                criteria.andEqualTo("num", searchMap.get("num"));
            }
            // 预警库存
            if (searchMap.get("alertNum") != null) {
                criteria.andEqualTo("alertNum", searchMap.get("alertNum"));
            }

        }
        return example;
    }

}
